package cn.donting.jme.swing.tab;

import cn.donting.jme.EditorContent;
import cn.donting.jme.expansion.attribute.AttributeGroup;
import cn.donting.jme.inspect.Inspect;
import cn.donting.jme.swing.Tab;
import cn.donting.jme.swing.drag.FileTransferable;
import cn.hutool.core.io.FileUtil;
import lombok.Getter;
import org.jdesktop.swingx.VerticalLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class InspectTab extends Tab {
    public static final int tabIndex = 1;
    JScrollPane jScrollPane;
    JPanel viewPane;

    private JComponent top;
    private JComponent bottom;
    @Getter
    private Inspect inspect;

    private boolean loading = false;
    private JLabel loadingJLabel;

    public InspectTab() {
        setTitle("检查");
        setLayout(new BorderLayout());
        viewPane = new JPanel();
        VerticalLayout verticalLayout = new VerticalLayout();
        viewPane.setLayout(verticalLayout);
        jScrollPane = new JScrollPane(viewPane);
        add(jScrollPane, BorderLayout.CENTER);
        jScrollPane.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                Dimension dimension = verticalLayout.preferredLayoutSize(viewPane);
                viewPane.setPreferredSize(new Dimension(0, dimension.height));
            }
        });
        // 获取垂直滚动条
        JScrollBar verticalScrollBar = jScrollPane.getVerticalScrollBar();
        // 设置单次滚动量
        verticalScrollBar.setUnitIncrement(10);


        setTransferHandler(new InspectTransferHandler());
        loadingJLabel = new JLabel("加载中...");
        loadingJLabel.setHorizontalAlignment(SwingConstants.CENTER);
    }

    public void addAttributeGroup(AttributeGroup attributeGroup) {
        Container uiContainer = attributeGroup.getUiContainer();
        uiContainer.setName(attributeGroup.getKey());
        viewPane.add(uiContainer);
        viewPane.revalidate();
        viewPane.repaint();
    }

    public void setAttributeGroup(Inspect inspect) {
        if (!EditorContent.isSwingThread()) {
            EditorContent.runSwing(() -> setAttributeGroup(inspect));
            return;
        }
        if (loading) {
            remove(loadingJLabel);
            add(jScrollPane, BorderLayout.CENTER);
            loading = false;
        }
        this.inspect = inspect;
        viewPane.removeAll();
        if (top != null) {
            remove(top);
            top = null;
        }
        if (bottom != null) {
            remove(bottom);
            bottom = null;
        }
        if (inspect == null) {
            revalidate();
            repaint();
            return;
        }
        List<AttributeGroup> attributeGroups = inspect.getAttributeGroups();
        for (AttributeGroup group : attributeGroups) {
            Container uiContainer = group.getUiContainer();
            viewPane.add(uiContainer);
        }
        if (inspect.getTop() != null) {
            top = inspect.getTop();
            add(inspect.getTop(), BorderLayout.NORTH);
        }
        if (inspect.getBottom() != null) {
            bottom = inspect.getBottom();
            add(inspect.getTop(), BorderLayout.SOUTH);
        }
        Object inspectObj = inspect.getInspectObj();
        String objectId = EditorContent.getObjectId(inspectObj);
        setTitle("检查"+objectId);
        revalidate();
        repaint();
    }

    public void setLoading() {
        loading = true;
        remove(jScrollPane);
        add(loadingJLabel, BorderLayout.CENTER);
        loadingJLabel.setText("加载中...");
    }

    public void updateGroup() {
        revalidate();
        repaint();
    }

    public void setLoadError(Exception ex) {
        setLoading();
        loadingJLabel.setText("加载失败");
    }


    private class InspectTransferHandler extends TransferHandler {

        @Override
        public boolean canImport(TransferHandler.TransferSupport support) {
            if (inspect == null) {
                return false;
            }
            for (TransferHandler transferHandler : inspect.getTransferHandlers()) {
                if (transferHandler.canImport(support)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public boolean importData(TransferSupport support) {
            for (TransferHandler transferHandler : inspect.getTransferHandlers()) {
                if (transferHandler.canImport(support)) {
                    return transferHandler.importData(support);
                }
            }
            return false;
        }
    }

}
